\\ Disabled

Optional Buffers.f

Requires Linked-lists
Requires Structs
Requires Kagi
Requires Tools

Only Forth Definitions

struct
  cell+  \ link
  cell field ->size
  cell field ->addr
end-struct /buffer

variable buffers

:macro >memory  ->addr @ ;

: buffer-allocate   ( size buffer -- )
  2dup ->size !
  swap cell+ allocate throw ( buffer addr )
  2dup !
  cell+ swap ->addr ! ;

: buffer,  ( size -- )
  here /buffer /allot dup dup buffers add-link buffer-allocate  ;

: do-buffer
   does> ->addr @ ;

-? : buffer:   ( size -- <name> )  ( -- addr )
   create buffer, do-buffer ;

\ These are internal

: >buffer   cell- @ ;

: buffer-free  ( buffer -- )
  dup ->addr @ ?dup if cell- free throw dup ->addr off then drop ;

: buffer-reallocate  ( buffer -- )
  dup buffer-free  dup ->size @ swap buffer-allocate ;

: free-buffers
  traverse:[ buffer-free ;] ;

: reallocate-buffers
   traverse:[ buffer-reallocate ;] ;

:prune   buffers free-buffers ;
\ :onload   buffers reallocate-buffers ;
:onexit   buffers free-buffers ;
